/* MainLayoutComposer.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Nov 12, 2008 3:10:06 PM , Created by jumperchen
}}IS_NOTE

Copyright (C) 2008 Potix Corporation. All Rights Reserved.

{{IS_RIGHT
	This program is distributed under GPL Version 3.0 in the hope that
	it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
 */
package foundation.common.menu.composer;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.zkoss.util.logging.Log;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.util.ComposerExt;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Treecell;

import foundation.common.util.CommonWebUtil;


@Controller
public class TabComposer extends GenericForwardComposer implements ComposerExt {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Log log = Log.lookup(TabComposer.class);
	

	
	public TabComposer() {
		
	}


	// Composer Implementation
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}

	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		return compInfo;
	}


	public boolean doCatch(Throwable ex) throws Exception {
		log.error("Failed to compose "+this, ex);
		return false;
	}

	public void doFinally() throws Exception {
	}

	@RequestMapping (value="/home.do")
	public void onSubMenuSelect(ForwardEvent event) {
		Executions.getCurrent().getDesktop().removeAttribute("objectToedit");
		Treecell cell = (Treecell) event.getOrigin().getTarget();	
		SubMenu subMenu= this.getSubMenu(cell.getId());
		CommonWebUtil.forwardPage(subMenu.getPath(),subMenu.getId(),subMenu.getLabel());
		//		CommonWebUtil.forward(URLdestino);		
	}
	
	public static Map getSubMenuMap() {
		return SubMenuWebAppInit.getSubMenuMap();
	}
	
	public static SubMenu getSubMenu(String submenuId) {
		return (SubMenu) getSubMenuMap().get(submenuId);
	}
}
